package com.wyj.longrangeclient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.wyj.longrangeclient.Service.ScreenRecorder;
import com.wyj.longrangeclient.Service.WebSocketService;
import com.wyj.longrangeclient.Service.YourAccessibilityService;
import com.wyj.longrangeclient.Util.RxBus;
import com.wyj.longrangeclient.Util.WebSocketUtil;

import java.util.List;

import rx.Observer;

public class MainActivity extends AppCompatActivity {

    private EditText edit;

    private View msg_view;
    private TextView msg_text;

    private TextView socket_text;
    private TextView screen_recorder_text;
    private TextView accessibility_text;

    private int REQUEST_CODE = 1001;
    private MediaProjectionManager mProjectionManager;

    private int stop;
    private int bj = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit = findViewById(R.id.edit);

        msg_view = findViewById(R.id.msg_view);
        msg_text = findViewById(R.id.msg_text);

        socket_text = findViewById(R.id.socket_text);
        screen_recorder_text = findViewById(R.id.screen_recorder_text);
        accessibility_text = findViewById(R.id.accessibility_text);

        msg_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!"".equals(edit.getText()+"")){

                    boolean isAccessibility = isServiceON(MainActivity.this, YourAccessibilityService.class.getName());

                    if (isAccessibility){

                        if (mProjectionManager==null){

                            mProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
                            startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
                        }

                        Intent intent = new Intent(MainActivity.this, WebSocketService.class);
                        intent.putExtra("url","ws://81.71.4.249:9999/rtsp_websocket?name=" + edit.getText());
//                        intent.putExtra("url","ws://192.168.8.9:9999/rtsp_websocket?name=" + edit.getText());
                        startService(intent);

                    }else {

                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }
            }
        });

    }


    private boolean isServiceON(Context context, String className){

        ActivityManager activityManager = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo>
                runningServices = activityManager.getRunningServices(100);
        if (runningServices.size() < 0 ){
            return false;
        }
        for (int i = 0;i<runningServices.size();i++){
            ComponentName service = runningServices.get(i).service;
            if (service.getClassName().contains(className)){
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (RESULT_OK == resultCode && REQUEST_CODE == requestCode){

            Intent service = new Intent(this, ScreenRecorder.class);
            service.putExtra("code", resultCode);
            service.putExtra("data", data);
            startForegroundService(service);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        RxBus.getInstance().Register("SocketMsg", new Observer<Object>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        msg_view.setVisibility(!"连接关闭".equals(o)?View.VISIBLE:View.GONE);
                        msg_text.setText(o+"");
                    }
                });
            }
        });

        RxBus.getInstance().Register("ServiceMsg", new Observer<Object>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

                String data = o + "";

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        stop = stop + 1;

                        if (stop>=300){

                            stop=0;

                            msg_view.setBackgroundColor(Color.parseColor(bj==0?"#263238":"#ffffff"));
                            socket_text.setTextColor(Color.parseColor(bj==0?"#ffffff":"#263238"));
                            screen_recorder_text.setTextColor(Color.parseColor(bj==0?"#ffffff":"#263238"));
                            accessibility_text.setTextColor(Color.parseColor(bj==0?"#ffffff":"#263238"));
                            msg_text.setTextColor(Color.parseColor(bj==0?"#ffffff":"#263238"));

                            bj = (bj==0)?1:0;
                        }

                        if (data.indexOf("网络服务")!=-1){

                            socket_text.setText(data);

                        }else if (data.indexOf("录屏服务")!=-1){

                            screen_recorder_text.setText(data);

                        }else if (data.indexOf("无障碍服务")!=-1){

                            accessibility_text.setText(data);
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        RxBus.getInstance().Cancellation("SocketMsg");
        RxBus.getInstance().Cancellation("ServiceMsg");
    }
}